﻿Public Class TrxStokOpnamePage
    Private Sub getnotrx()
        txtnopo.Text = ""
        Dim dt As New DataTable
        If Utils.GetFunction("getidtrx", New Object() {"so"}, dt) Then
            If Utils.RowExist(dt) Then
                txtnopo.Text = dt(0)(0)
            End If
        End If
    End Sub
    Private Sub getGudang()
        cmbgudang.Items.Clear()
        Dim dt As New DataTable
        dt = Utils.SearchAllLike("rsitemlocation", "itemlocation", "")
        If Utils.RowExist(dt) Then

            For Each r As DataRow In dt.Rows
                cmbgudang.Items.Add(r("itemlocation").ToString)
                cmbgudang.SelectedIndex = 0
            Next
        End If
    End Sub

    Private Sub TrxStokOpnamePage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getnotrx()
        GridTransaksi.Rows.Clear()
        dtpTrans.Value = Now
        getGudang()
    End Sub

    Private Sub TrxStokOpnamePage_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.VisibleChanged
        getnotrx()
        GridTransaksi.Rows.Clear()
        dtpTrans.Value = Now
        getGudang()
    End Sub

    Private Sub GridTransaksi_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles GridTransaksi.EditingControlShowing
        If GridTransaksi.CurrentCell.ColumnIndex = 4 Then
            Dim TextBox1 As TextBox
            TextBox1 = CType(e.Control, TextBox)
            RemoveHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
            AddHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
        ElseIf GridTransaksi.CurrentCell.ColumnIndex = 0 Then
            Dim TextBox1 As TextBox
            TextBox1 = CType(e.Control, TextBox)
            RemoveHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
            'AddHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox1_KeyPress)
        End If
    End Sub
    Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = Utils.HanyaAngka(Asc(e.KeyChar))
    End Sub

    Private Function validasirowgrid(ByVal dgr As DataGridView) As Boolean
        Dim retval As Boolean = True
        If GridTransaksi.RowCount > 0 Then
            If GridTransaksi.RowCount = 1 And GridTransaksi.Rows(0).IsNewRow Then
                retval = False
            End If
        Else
            retval = False
        End If
        Return retval


    End Function

    Private Sub GridTransaksi_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridTransaksi.KeyDown
        If e.KeyValue = Keys.Delete Then
            Try
                Dim dgr As New DataGridViewRow
                dgr = GridTransaksi.CurrentRow
                GridTransaksi.Rows.Remove(dgr)
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub GridTransaksi_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridTransaksi.KeyUp
        Dim ic, ir As Integer
        If validasirowgrid(GridTransaksi) Then
            ic = GridTransaksi.CurrentCell.ColumnIndex
            ir = GridTransaksi.CurrentCell.RowIndex
        Else
            If Not GridTransaksi.CurrentCell Is Nothing Then
                GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.CurrentCell.RowIndex)
                GridTransaksi.CurrentCell.Selected = True
            End If

            Exit Sub
        End If

        If e.KeyValue = 13 Then
            If GridTransaksi.CurrentCell.ColumnIndex = 1 Or GridTransaksi.CurrentCell.ColumnIndex = 2 Then
                If Not IsDBNull(GridTransaksi(0, ir).Value) Or Not IsDBNull(GridTransaksi.CurrentCell.Value) Then
                    If Not GridTransaksi(0, ir).Value = "" Or Not GridTransaksi(1, ir).Value = "" Then

                        GridTransaksi.CurrentCell = GridTransaksi(4, ir)
                        GridTransaksi.CurrentCell.Selected = True

                        'If Not IsDBNull(GridTransaksi(4, ir).Value) Then
                        '    If GridTransaksi(4, ir).Value = 0 Then
                        '        'GridTransaksi(4, ir).Value = 0
                        '        'GridTransaksi(5, ir).Value = GridTransaksi(3, ir).Value * GridTransaksi(2, ir).Value
                        '        'sumtotal()
                        '        'sumhutang()
                        '        GridTransaksi.CurrentCell = GridTransaksi(4, ir)
                        '        GridTransaksi.CurrentCell.Selected = True
                        '    ElseIf GridTransaksi(4, ir).Value > 0 Then

                        '        GridTransaksi.CurrentCell = GridTransaksi(4, ir)
                        '        GridTransaksi.CurrentCell.Selected = True
                        '    End If
                        'Else
                        '    GridTransaksi.CurrentCell = GridTransaksi(4, ir)
                        '    GridTransaksi.CurrentCell.Selected = True
                        'End If

                    Else
                        GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                        GridTransaksi.CurrentCell.Selected = True
                    End If
                Else
                    GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                    GridTransaksi.CurrentCell.Selected = True
                End If

                'Dim eKey As New KeyEventArgs(Keys.Return)
                'GridTransaksi_KeyUp(sender, eKey)
            ElseIf GridTransaksi.CurrentCell.ColumnIndex = 4 Then
                If GridTransaksi(4, ir).Value Is Nothing Then
                    GridTransaksi.CurrentCell.Value = 0
                    'GridTransaksi.CurrentCell.Selected = True
                End If
            ElseIf GridTransaksi.CurrentCell.ColumnIndex = 5 Then
                If GridTransaksi(4, ir).Value Is Nothing Or GridTransaksi(4, ir).Value.ToString = "" Then
                    GridTransaksi(4, ir).Value = GridTransaksi(3, ir).Value
                    'GridTransaksi.CurrentCell.Selected = True
                End If
                GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                GridTransaksi.CurrentCell.Selected = True
            End If
        End If
    End Sub

    Private Sub GridTransaksi_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridTransaksi.CellEndEdit
        If GridTransaksi.CurrentCell.ColumnIndex = 0 Then
            If Not IsDBNull(GridTransaksi.CurrentCell) Then
                If Not GridTransaksi.CurrentCell.Value = "" Then
                    'blm kelar=? masukkan search equal
                    Dim dt As New DataTable
                    dt = Utils.SearchAllWhere("vstokaktif", "iditem=""" & GridTransaksi.CurrentCell.Value & """ and itemlocation=""" & cmbgudang.Text & """")
                    If Utils.RowExist(dt) Then
                        GridTransaksi(1, e.RowIndex).Value = IIf(IsDBNull(dt(0)("namaitem")), "", dt(0)("namaitem"))
                        GridTransaksi(2, e.RowIndex).Value = IIf(IsDBNull(dt(0)("satuanjual")), "", dt(0)("satuanjual"))
                        GridTransaksi(3, e.RowIndex).Value = IIf(IsDBNull(dt(0)("qtyakhir")), "", dt(0)("qtyakhir"))
                        GridTransaksi(4, e.RowIndex).Value = IIf(IsDBNull(dt(0)("qtyakhir")), "", dt(0)("qtyakhir"))
                        GridTransaksi(5, e.RowIndex).Value = GridTransaksi(3, e.RowIndex).Value - GridTransaksi(4, e.RowIndex).Value

                    Else
                        MsgBox("data tidak terdaftar")
                        GridTransaksi.Rows.RemoveAt(e.RowIndex)
                        GridTransaksi.CurrentCell = GridTransaksi.Item(0, e.RowIndex)
                        GridTransaksi.Item(0, e.RowIndex).Selected = True
                    End If
                End If
            End If
            For Each dgr As DataGridViewRow In GridTransaksi.Rows
                If dgr.Index <> GridTransaksi.CurrentCell.RowIndex Then
                    If dgr.Cells(0).Value = GridTransaksi.CurrentCell.Value Then
                        'dgr.Cells(3).Value = dgr.Cells(3).Value + 1
                        'dgr.Cells(5).Value = dgr.Cells(3).Value * dgr.Cells(2).Value

                        'GridTransaksi.Rows.Remove(GridTransaksi.CurrentRow)
                        'sumtotal()

                    End If
                End If

            Next
        ElseIf GridTransaksi.CurrentCell.ColumnIndex = 4 Then
            If Not IsDBNull(GridTransaksi.CurrentCell) Then
                'If Not GridTransaksi.CurrentCell.Value.ToString = "" Then
                Try
                    If GridTransaksi.CurrentCell.Value.ToString = "" Then
                        GridTransaksi.CurrentCell.Value = 0
                    End If
                    GridTransaksi(5, e.RowIndex).Value = GridTransaksi(3, e.RowIndex).Value - GridTransaksi(4, e.RowIndex).Value
                Catch ex As Exception
                    GridTransaksi.CurrentCell.Value = GridTransaksi(3, e.RowIndex).Value
                    GridTransaksi(5, e.RowIndex).Value = GridTransaksi(3, e.RowIndex).Value - GridTransaksi(4, e.RowIndex).Value
                End Try

                'sumtotal()

                'End If
            End If
        End If
    End Sub

    Private Sub CariitemToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CariitemToolStripMenuItem.Click
        Dim DialogSearchItem As New DialogSearchItem
        DialogSearchItem.TableKategori = "vstokaktifall"
        DialogSearchItem.ShowDialog()
        Dim dgr As New DataGridViewRow
        dgr = DialogSearchItem.ReturnValue
        If dgr Is Nothing Then
            Exit Sub
        End If
        Dim str As String
        str = IIf(IsDBNull(dgr.Cells(0).Value), "", dgr.Cells(0).Value)
        If str = String.Empty Then
            Exit Sub
        Else
            GridTransaksi.Rows.Add()
            GridTransaksi(0, GridTransaksi.RowCount - 2).Value = str
            'GridTransaksi(1, GridTransaksi.RowCount - 2).Value = IIf(IsDBNull(dgr.Cells(1).Value), "", dgr.Cells(1).Value)
            'GridTransaksi(3, GridTransaksi.RowCount - 2).Value = 1
            GridTransaksi.Focus()
            GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 2)
            GridTransaksi(0, GridTransaksi.RowCount - 2).Selected = True
            GridTransaksi.BeginEdit(True)
            GridTransaksi.EndEdit(True)
        End If
    End Sub

    Private Sub AktifgridToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AktifgridToolStripMenuItem.Click
        GridTransaksi.Focus()
    End Sub

    Private Sub FocusupplierToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FocusupplierToolStripMenuItem.Click
        cmbgudang.Focus()
    End Sub

    Private Sub RefreshToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshToolStripMenuItem.Click

        GridTransaksi.Rows.Clear()
        'setCombo()
        getnotrx()
        dtpTrans.Value = Now
        getGudang()
    End Sub

    Private Sub SimpanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpanToolStripMenuItem.Click
        Dim retval As Boolean = True
        If cmbgudang.Text = "" Then
            retval = False
        ElseIf GridTransaksi.RowCount - 1 < 1 Then
            retval = False
        End If
        If Not retval Then
            MsgBox("Maaf Data Entry Tidak Valid")
            Exit Sub
        End If

        getnotrx()


        Dim dt As New DataTable
        Dim str As String = ""

        'If Utils.RowExist(dt) Then

        str = "Execute Finish"
        'End If
        Dim strdata As String = ""
        strdata = Utils.GridValueColumnToText(GridTransaksi, New Object() {"iditem", "namaitem", "satuan", "qty", "qtyfisik", "selisih"})
        Dim params As String()

        If Not strdata = "" Then
            params = strdata.Split("|")
            For int As Integer = 0 To params.Length - 1
                params(int) = params(int).Insert(0, cmbgudang.Text & ",")
                params(int) = params(int).Insert(0, dtpTrans.Value.ToString("yyyy-MM-dd") & ",")
                params(int) = params(int).Insert(0, txtnopo.Text & ",")
                params(int) = params(int).Insert(params(int).Length, "," & LocalSession.operatorname)

            Next
            strdata = String.Join("|", params)
            If strdata.EndsWith("|") Then
                strdata = LSet(strdata, strdata.Length - 1)
            End If
            Dim dtretval As New DataTable
            If Utils.executeSP("proc_stokopname", New Object() {txtnopo.Text, GridTransaksi.RowCount - 1, 10, strdata}, dtretval) Then
                If Utils.RowExist(dtretval) Then
                    str = dtretval(0)(0)
                End If
                MsgBox(str)
                If Not dtretval(0)(1) = 4 Then
                    If Utils.exec_SP("proc_setstokexec", New Object() {txtnopo.Text}) Then

                    End If
                    GridTransaksi.Rows.Clear()
                    getnotrx()
                End If
            End If
        End If
    End Sub
End Class
